package com.zmn.brs.common.enums;

import java.util.Optional;
import java.util.stream.Stream;

/**
 * 类描述：事件枚举
 *
 * @author xujie
 * @since 2021/03/15 14:41
 * @modifier zhangyao 2021/04/26 增加枚举值
 */
public enum EventEnum {

    /**
     * 登录
     */
    ADMIN_LOGIN(10000000, "运营后台登录"),
    // CC座席端登录 使用的网关访问，事件编码变更需通知
    CC_SEAT_END_LOGIN(10000001, "CC座席端登录"),
    CC_ADMIN_LOGIN(10000002, "CC管理端登录"),
    // 工程师APP登录 使用的dubbo接口请求，事件编码变更需通知
    ENGINEER_APP_LOGIN(10000003, "工程师APP登录"),

    /**
     * 导出
     */
    WORK_ORDER_EXPORT(20000000, "工单导出"),
    ZMN_WORK_ORDER_EXPORT(20000010, "啄木鸟工单导出"),
    YEYX_WORK_ORDER_EXPORT(20000020, "言而有信工单导出"),
    SAAS_WORK_ORDER_EXPORT(20000030, "一步维修工单导出"),
    CNHB_WORK_ORDER_EXPORT(20000040, "川南环保工单导出"),

    INCREMENT_WORK_ORDER_EXPORT(20001000, "增值工单导出"),
    ENGINEER_EXPORT(20002000, "工程师导出"),
    CONSUMER_EXPORT(20003000, "C端用户导出"),
    BUSINESS_EXPORT(20004000, "B端用户导出"),
    PASS_EXPORT(20005000, "通行证导出"),
    /**
     * 查询
     */
    WORK_ORDER_QUERY(30000000, "运营后台工单查询"),
    ZMN_WORK_ORDER_QUERY(30000010, "啄木鸟工单查询"),
    YEYX_WORK_ORDER_QUERY(30000020, "言而有信工单查询"),
    SAAS_WORK_ORDER_QUERY(30000030, "一步维修工单查询"),
    CNHB_WORK_ORDER_QUERY(30000040, "川南环保工单查询"),

    INCREMENT_WORK_ORDER_QUERY(30001000, "运营后台增值工单查询"),
    ENGINEER_QUERY(30002000, "工程师查询"),
    PAY_RECORD_QUERY(30003000, "支付报表/记录查询"),
    REPORT_QUERY(30004000, "报表查询"),

    /**
     * 前台下单
     */
    VALUE_ADDED_WORK_ORDER_PLACE_AN_ORDER(40000000, "增值工单下单"),
    /**
     * 工单管理
     */
    QUICK_PLACE_AN_ORDER(50000000, "快捷下单"),
    ZMN_WORK_ORDER_PLACE_AN_ORDER(50000010, "啄木鸟工单下单"),
    YEYX_WORK_ORDER_PLACE_AN_ORDER(50000020, "言而有信工单下单"),
    SAAS_WORK_ORDER_PLACE_AN_ORDER(50000030, "一步维修工单下单"),
    CNHB_WORK_ORDER_PLACE_AN_ORDER(50000040, "川南环保工单下单"),
    // 啄木鸟工单取消、言而有信工单取消、一步维修工单取消、川南环保工单取消的总编码，编码50001000 + 啄木鸟平台10 = 啄木鸟工单取消50001010
    WORK_ORDER_CANCEL(50001000, "取消"),
    ZMN_WORK_ORDER_CANCEL(50001010, "啄木鸟工单取消"),
    YEYX_WORK_ORDER_CANCEL(50001020, "言而有信工单取消"),
    SAAS_WORK_ORDER_CANCEL(50001030, "一步维修工单取消"),
    CNHB_WORK_ORDER_CANCEL(50001040, "川南环保工单取消"),
    /**
     * 增值工单管理
     */
    VALUE_ADDED_WORK_ORDER_CANCEL(60000000, "取消");

    /**
     * 事件编码
     */
    private final Integer eventCode;

    /**
     * 事件名
     */
    private final String eventName;


    EventEnum(Integer eventCode, String eventName) {

        this.eventCode = eventCode;
        this.eventName = eventName;
    }

    public Integer getEventCode() {

        return eventCode;
    }

    public String getEventName() {

        return eventName;
    }

    public static EventEnum getEventEnum(Integer eventCode) {

        Optional<EventEnum> optional = Stream.of(EventEnum.values()).filter(e -> e.getEventCode().equals(eventCode)).findFirst();
        return optional.orElse(null);
    }

    /**
     * 事件枚举转换
     *
     * @param eventEnum 事件枚举
     * @param plat 平台 10.啄木鸟 20.言而有信 30.一步维修 40.川南环保
     * @return 事件枚举
     */
    public static EventEnum transformEventEnum(EventEnum eventEnum, Integer plat) {

        if (eventEnum == null) {
            return eventEnum;
        }
        if (!WORK_ORDER_EXPORT.equals(eventEnum) && !WORK_ORDER_QUERY.equals(eventEnum)
                && !QUICK_PLACE_AN_ORDER.equals(eventEnum) && !WORK_ORDER_CANCEL.equals(eventEnum)) {
            return eventEnum;
        }
        if (!QUICK_PLACE_AN_ORDER.equals(eventEnum) && plat == null) {
            // 如果编码是快捷下单，plat为null时为快捷下单，plat有值时为对应平台下单
            return eventEnum;
        }
        Integer eventCode = eventEnum.getEventCode();
        if (plat != null) {
            eventCode += plat;
        }
        EventEnum transformEventEnum = getEventEnum(eventCode);
        if (transformEventEnum != null) {
            return transformEventEnum;
        } else {
            return eventEnum;
        }
    }

    @Override
    public String toString() {

        return "EventEnum{" +
                "eventCode=" + eventCode +
                ", eventName='" + eventName + '\'' +
                '}';
    }
}
